/*
 * MP3_Beaney - The MP3 Tag Editor is a free ID3-Tag Editor
 * Copyright (C) 2012 Bernhard Teismann, André Seipelt, Steffen Gruschka, Imanuel Rohlfing
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package uInterface;

import com.jtattoo.plaf.smart.SmartLookAndFeel;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import model.MP3Exception;
import model.MP3Management;

/**
 * The MainFrame Class which is built on Programm Start.
 *
 * @author Steffen Gruschka
 * @author Bernhard Teismann
 * @author André Seipelt
 * @author Imanuel Rohlfing
 * @version 1.0
 */
public class MainInterface extends javax.swing.JFrame {

    /**
     * Constructor. Creates a new MainInterface.
     */
    public MainInterface(){
        initComponents();
        extendedInit();
        initImages();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TabbedPanel = new javax.swing.JTabbedPane();
        step11 = new uInterface.Step1();
        step31 = new uInterface.Step3();
        step41 = new uInterface.Step4();
        step51 = new uInterface.Step5();
        bannerPannel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        MainHelpButton = new javax.swing.JButton();
        MainNextButton = new javax.swing.JButton();
        MainBackButton = new javax.swing.JButton();
        MainCloseButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("MP3-Beaney - Der Tag Editor");
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(244, 242, 232));
        setBounds(new java.awt.Rectangle(150, 50, 950, 15));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImages(null);
        setMinimumSize(new java.awt.Dimension(800, 615));
        setResizable(false);

        TabbedPanel.setBackground(new java.awt.Color(255, 255, 255));
        TabbedPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        TabbedPanel.setFocusable(false);
        TabbedPanel.setPreferredSize(new java.awt.Dimension(1100, 400));

        step11.setMinimumSize(new java.awt.Dimension(850, 400));
        TabbedPanel.addTab("Datei oder Ordner Öffnen", step11);

        step31.setMinimumSize(new java.awt.Dimension(987, 430));
        TabbedPanel.addTab("Tags bearbeiten", step31);
        TabbedPanel.addTab("Umbenennen", step41);
        TabbedPanel.addTab("Fertig", step51);

        bannerPannel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        bannerPannel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bannerPannelMouseClicked(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Banner.png"))); // NOI18N

        org.jdesktop.layout.GroupLayout bannerPannelLayout = new org.jdesktop.layout.GroupLayout(bannerPannel);
        bannerPannel.setLayout(bannerPannelLayout);
        bannerPannelLayout.setHorizontalGroup(
            bannerPannelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, bannerPannelLayout.createSequentialGroup()
                .add(0, 25, Short.MAX_VALUE)
                .add(jLabel1))
        );
        bannerPannelLayout.setVerticalGroup(
            bannerPannelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, bannerPannelLayout.createSequentialGroup()
                .add(0, 0, Short.MAX_VALUE)
                .add(jLabel1))
        );

        MainHelpButton.setFont(new java.awt.Font("Lucida Grande", 0, 16)); // NOI18N
        MainHelpButton.setText("Hilfe");
        MainHelpButton.setMaximumSize(new java.awt.Dimension(75, 40));
        MainHelpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MainHelpButtonActionPerformed(evt);
            }
        });

        MainNextButton.setFont(new java.awt.Font("Lucida Grande", 0, 16)); // NOI18N
        MainNextButton.setText("Weiter >");
        MainNextButton.setMaximumSize(new java.awt.Dimension(83, 40));
        MainNextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MainNextButtonActionPerformed(evt);
            }
        });

        MainBackButton.setFont(new java.awt.Font("Lucida Grande", 0, 16)); // NOI18N
        MainBackButton.setText("< Zurück");
        MainBackButton.setMaximumSize(new java.awt.Dimension(88, 40));
        MainBackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MainBackButtonActionPerformed(evt);
            }
        });

        MainCloseButton.setFont(new java.awt.Font("Lucida Grande", 0, 16)); // NOI18N
        MainCloseButton.setText("Beenden");
        MainCloseButton.setMaximumSize(new java.awt.Dimension(96, 40));
        MainCloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MainCloseButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(bannerPannel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(MainHelpButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 281, Short.MAX_VALUE)
                        .add(MainBackButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(MainNextButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(274, 274, 274)
                        .add(MainCloseButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(TabbedPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(bannerPannel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(TabbedPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 416, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(MainHelpButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(MainBackButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(MainNextButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(MainCloseButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /*
     * Init for player, Tabbed Panel, all steps and the Tabbed Panel
     */

    private void extendedInit(){
        MainBackButton.setEnabled(false);
        step11.setMainInterface(this);
        step31.setMainInterface(this);
        step41.setMainInterface(this);
        step51.setMainInterface(this);

        TabbedPanel.setEnabledAt(0, true);
        TabbedPanel.setEnabledAt(1, false);
        TabbedPanel.setEnabledAt(2, false);
        TabbedPanel.setEnabledAt(3, false);
        this.setAlwaysOnTop(false);
    }

    /*
     * Sets the IconImage for the mainframe to a custom one
     */
    private void initImages(){
        Image symbol = Toolkit.getDefaultToolkit().getImage("src/images/Success.png");
        setIconImage(symbol);
    }

    /**
     * Sets the Programm back to step One with startup-configuration
     */
    public void goToStart(){
        MainBackButton.setEnabled(false);
        MainNextButton.setEnabled(true);
        TabbedPanel.setEnabledAt(0, true);
        TabbedPanel.setEnabledAt(1, false);
        TabbedPanel.setEnabledAt(2, false);
        TabbedPanel.setEnabledAt(3, false);
        TabbedPanel.setSelectedIndex(0);
    }

    private void MainCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MainCloseButtonActionPerformed
        System.exit(0);
    }//GEN-LAST:event_MainCloseButtonActionPerformed

    /*
     * Provides Functionality for the Next-Button in all Steps and wheather its activaed or not.
     * Also sets Tabbed Panel to be enabled or disabled.
     */
    private void MainNextButtonActionPerformed(java.awt.event.ActionEvent evt){//GEN-FIRST:event_MainNextButtonActionPerformed
        switch (TabbedPanel.getSelectedIndex()){
            case 0:
                try{
                    manager = null;
                    manager = new MP3Management(step11.getTextFromTextfild());
                    TabbedPanel.setEnabledAt(0, false);
                    TabbedPanel.setEnabledAt(1, true);
                    TabbedPanel.setSelectedIndex(1);
                    MainBackButton.setEnabled(true);
                    step31.setMP3Manager(manager);
                    step51.setMP3Manager(manager);
                    step31.updateTable();
                }catch (MP3Exception e){
                    JOptionPane.showMessageDialog(null, e.get(), Messages.HEADLINE_ERR, JOptionPane.ERROR_MESSAGE, createIcon(Messages.IMAGE_FAULT));
                }
                break;
            case 1:
                TabbedPanel.setEnabledAt(1, false);
                TabbedPanel.setEnabledAt(2, true);
                TabbedPanel.setSelectedIndex(2);
                step31.leaveTableFront();
                break;
            case 2:
                if (JOptionPane.showConfirmDialog(null, Messages.STEP_4_CONFIRM, Messages.HEADLINE_CONF, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, createIcon(Messages.IMAGE_HELP)) == JOptionPane.OK_OPTION){
                    try{
                        manager.save();
                        if (step41.isData()){
                            manager.setFileNames(step41.getDataString());
                        }
                        if (step41.isOrder()){
                            manager.setPath(step41.getOrderString());
                        }
                        TabbedPanel.setEnabledAt(2, false);
                        TabbedPanel.setEnabledAt(3, true);
                        TabbedPanel.setSelectedIndex(3);
                        MainNextButton.setEnabled(false);
                        MainBackButton.setEnabled(false);
                    }catch (MP3Exception e){
                        JOptionPane.showMessageDialog(null, e.get(), Messages.HEADLINE_ERR, JOptionPane.ERROR_MESSAGE, createIcon(Messages.IMAGE_FAULT));
                    }
                }
                break;
            default:
                JOptionPane.showMessageDialog(null, Messages.BUTTON_SHOULD_NOT_BE_CLICKABLE, Messages.HEADLINE_ERR, JOptionPane.ERROR_MESSAGE, createIcon(Messages.IMAGE_FAULT));
        }
    }//GEN-LAST:event_MainNextButtonActionPerformed

    /*
     * Sets up the Back-Buttonaction and when it is enabled
     */
    private void MainBackButtonActionPerformed(java.awt.event.ActionEvent evt){//GEN-FIRST:event_MainBackButtonActionPerformed
        switch (TabbedPanel.getSelectedIndex()){
            case 1:
                if (JOptionPane.showConfirmDialog(null, Messages.BACK_CONFIRM, Messages.HEADLINE_CONF, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, createIcon(Messages.IMAGE_HELP)) == JOptionPane.OK_OPTION){

                    step31.leaveTableBack();
                    MainBackButton.setEnabled(false);
                    TabbedPanel.setSelectedIndex(0);
                    TabbedPanel.setEnabledAt(0, true);
                    TabbedPanel.setEnabledAt(1, false);
                }
                break;
            case 2:
                TabbedPanel.setSelectedIndex(1);
                TabbedPanel.setEnabledAt(1, true);
                TabbedPanel.setEnabledAt(2, false);
                break;
            default:
                JOptionPane.showMessageDialog(null, Messages.BUTTON_SHOULD_NOT_BE_CLICKABLE, Messages.HEADLINE_ERR, JOptionPane.ERROR_MESSAGE, createIcon(Messages.IMAGE_FAULT));
        }
    }//GEN-LAST:event_MainBackButtonActionPerformed

    /*
     * Defines help Button for step 1-4
     */
    private void MainHelpButtonActionPerformed(java.awt.event.ActionEvent evt){//GEN-FIRST:event_MainHelpButtonActionPerformed
        switch (TabbedPanel.getSelectedIndex()){
            case 0:
                JOptionPane.showMessageDialog(null, Messages.HELP_STEP_1, Messages.HEADLINE_HELP, JOptionPane.INFORMATION_MESSAGE, createIcon(Messages.IMAGE_HELP));
                break;
            case 1:
                JOptionPane.showMessageDialog(null, Messages.HELP_STEP_3, Messages.HEADLINE_HELP, JOptionPane.INFORMATION_MESSAGE, createIcon(Messages.IMAGE_HELP));
                break;
            case 2:
                JOptionPane.showMessageDialog(null, Messages.HELP_STEP_4, Messages.HEADLINE_HELP, JOptionPane.INFORMATION_MESSAGE, createIcon(Messages.IMAGE_HELP));
                break;
            case 3:
                JOptionPane.showMessageDialog(null, Messages.HELP_STEP_5, Messages.HEADLINE_HELP, JOptionPane.INFORMATION_MESSAGE, createIcon(Messages.IMAGE_HELP));
                break;
            default:
                JOptionPane.showMessageDialog(null, Messages.HELP_ERR, Messages.HEADLINE_ERR, JOptionPane.ERROR_MESSAGE);
                break;
        }
    }//GEN-LAST:event_MainHelpButtonActionPerformed

    private void bannerPannelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bannerPannelMouseClicked
        JOptionPane.showMessageDialog(null, Messages.ABOUT_TEXT, Messages.HEADLINE_ABOUT, JOptionPane.INFORMATION_MESSAGE, createIcon(Messages.IMAGE_ABOUT));
    }//GEN-LAST:event_bannerPannelMouseClicked

    private ImageIcon createIcon(String path){
        java.net.URL imgUrl = getClass().getResource(path);
        if (imgUrl != null){
            return new ImageIcon(imgUrl);
        }else{
            return null;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]){
        /*
         * Setting Properties for look and feel
         */
        Properties props = new Properties();

        props.put("logoString", "my company");
        props.put("licenseKey", "INSERT YOUR LICENSE KEY HERE");

        props.put("selectionBackgroundColor", "209 201 161");
        props.put("menuSelectionBackgroundColor", "244 242 232");

        props.put("controlColor", "209 201 161");
        props.put("controlColorLight", "244 242 232");
        props.put("controlColorDark", "209 201 161");

        props.put("buttonColor", "244 242 232");
        props.put("buttonColorLight", "244 242 232");
        props.put("buttonColorDark", "209 201 161");

        props.put("rolloverColor", "244 242 232");
        props.put("rolloverColorLight", "244 242 232");
        props.put("rolloverColorDark", "112 72 54");

        props.put("windowTitleForegroundColor", "112 72 54");
        props.put("windowTitleBackgroundColor", "180 240 197");
        props.put("windowTitleColorLight", "209 201 161");
        props.put("windowTitleColorDark", "244 242 232");
        props.put("windowBorderColor", "112 72 54");

        // set your theme
        SmartLookAndFeel.setCurrentTheme(props);


        /*
         * Setting the Smart Look and feel from JTattoo Library (JTattoo.net). It is free to use for any
         * as freeware developed program.
         */
        try{
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()){
                javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
            }
        }catch (ClassNotFoundException ex){
            java.util.logging.Logger.getLogger(MainInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }catch (InstantiationException ex){
            java.util.logging.Logger.getLogger(MainInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }catch (IllegalAccessException ex){
            java.util.logging.Logger.getLogger(MainInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }catch (javax.swing.UnsupportedLookAndFeelException ex){
            java.util.logging.Logger.getLogger(MainInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run(){
                new MainInterface().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton MainBackButton;
    private javax.swing.JButton MainCloseButton;
    private javax.swing.JButton MainHelpButton;
    private javax.swing.JButton MainNextButton;
    private javax.swing.JTabbedPane TabbedPanel;
    private javax.swing.JPanel bannerPannel;
    private javax.swing.JLabel jLabel1;
    private uInterface.Step1 step11;
    private uInterface.Step3 step31;
    private uInterface.Step4 step41;
    private uInterface.Step5 step51;
    // End of variables declaration//GEN-END:variables
    private MP3Management manager;
    private JFrame about;
    private static UIManager uiMan;
}
